package LC83;

class Solution {
    //方法1
    public ListNode deleteDuplicates1(ListNode head) {
        if (head == null || head.next == null) { //节点数 < 2
            return head;
        }
        // 节点数 >= 2
        ListNode p1 = head;
        ListNode p2;
        while ((p2 = p1.next) != null) {
            if (p1.val == p2.val) { //相等删除p2
                p1.next = p2.next;
            } else {
                p1 = p1.next;
            }
        }
        return head;
    }

    //方法2
    public ListNode deleteDuplicates(ListNode head) {
        //节点数 < 2
        if (head == null || head.next == null) {
            return head;
        }
        //节点数 >= 2  递归
        ListNode p = head;
        if (p.val == p.next.val) {
            return deleteDuplicates(p.next);  //p.next没更新也就是没把重复的链接起来
        } else {
            p.next = deleteDuplicates(p.next);  //p.next更新  与下一个不重复的节点链接起来
            return p;  //返回我
        }

    }
}
